/**
 * Author : Jorge Mejuto Create : 05/11/2009
 */

package net.eyra.negocio.dao;

import java.util.List;

import net.eyra.negocio.entidades.gestion.propietarios.afeccion.Acuerdo;
import net.eyra.negocio.entidades.gestion.propietarios.afeccion.Afeccion;
import net.eyra.negocio.entidades.gestion.propietarios.afeccion.TipoAfeccion;
import net.eyra.negocio.entidades.gestion.propietarios.afeccion.Uso;
import net.eyra.negocio.entidades.gestion.propietarios.fincas.Finca;

import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.criterion.Restrictions;

public class InstalacionDao {

    public List<Finca> CargarFincasPorParque(int id_parque) throws EyraDaoException {
        Session session = null;
        List<Finca> fincas = null;
        try {
            session = HibernateUtil.getSession();
            session.beginTransaction().begin();
            fincas = session.createCriteria(Finca.class).add(Restrictions.eq("parque.id_parque", id_parque)).list();
            session.beginTransaction().commit();
        } catch (HibernateException e) {
            if (session != null) session.beginTransaction().rollback();
            throw new EyraDaoException(e);
        } finally {
            if (session != null && session.isOpen()) session.close();
        }
        return fincas;
    }

    public Finca getFinca(int id_finca) throws EyraDaoException {
        Session session = null;
        Finca finca = null;
        try {
            session = HibernateUtil.getSession();
            session.beginTransaction().begin();
            finca = (Finca) session.createCriteria(Finca.class)
                    .add(Restrictions.eq("id_finca", id_finca))
                    .uniqueResult();
            session.beginTransaction().commit();
        } catch (HibernateException e) {
            if (session != null) session.beginTransaction().rollback();
            throw new EyraDaoException(e);
        } finally {
            if (session != null && session.isOpen()) session.close();
        }
        return finca;
    }

    public Afeccion getAfeccion(int id_afeccion) throws EyraDaoException {
        Session session = null;
        Afeccion afeccion = null;
        try {
            session = HibernateUtil.getSession();
            session.beginTransaction().begin();
            afeccion = (Afeccion) session.createCriteria(Afeccion.class)
                    .add(Restrictions.eq("id_afeccion", id_afeccion))
                    .uniqueResult();
            session.beginTransaction().commit();
        } catch (HibernateException e) {
            if (session != null) session.beginTransaction().rollback();
            throw new EyraDaoException(e);
        } finally {
            if (session != null && session.isOpen()) session.close();
        }
        return afeccion;
    }

    public void setAfeccion(Afeccion afeccion) throws EyraDaoException {
        Session session = null;
        try {
            session = HibernateUtil.getSession();
            session.beginTransaction().begin();

            session.saveOrUpdate(afeccion);
            session.flush();
            session.beginTransaction().commit();
        } catch (HibernateException e) {
            if (session != null) session.beginTransaction().rollback();
            throw new EyraDaoException(e);
        } finally {
            if (session != null && session.isOpen()) session.close();
        }
    }

    public void BorrarAfeccion(Afeccion afeccion) throws EyraDaoException {
        Session session = null;
        try {
            session = HibernateUtil.getSession();
            session.beginTransaction().begin();

            session.delete(afeccion);
            session.flush();
            session.beginTransaction().commit();
        } catch (HibernateException e) {
            if (session != null) session.beginTransaction().rollback();
            throw new EyraDaoException(e);
        } finally {
            if (session != null && session.isOpen()) session.close();
        }
    }

    public List<Acuerdo> CargarAcuerdosPorParque(int id_parque) throws EyraDaoException {
        Session session = null;
        List<Acuerdo> acuerdos = null;
        try {
            session = HibernateUtil.getSession();
            session.beginTransaction().begin();
            acuerdos = session.createCriteria(Acuerdo.class).add(Restrictions.eq("parque.id_parque", id_parque)).list();
            session.beginTransaction().commit();
        } catch (HibernateException e) {
            if (session != null) session.beginTransaction().rollback();
            throw new EyraDaoException(e);
        } finally {
            if (session != null && session.isOpen()) session.close();
        }
        return acuerdos;
    }

    public List<TipoAfeccion> getTiposAfeccion() throws EyraDaoException {
        Session session = null;
        List<TipoAfeccion> tipo_afecciones = null;
        try {
            session = HibernateUtil.getSession();
            session.beginTransaction().begin();
            tipo_afecciones = session.createCriteria(TipoAfeccion.class).list();
            session.beginTransaction().commit();
        } catch (HibernateException e) {
            if (session != null) session.beginTransaction().rollback();
            throw new EyraDaoException(e);
        } finally {
            if (session != null && session.isOpen()) session.close();
        }
        return tipo_afecciones;
    }

    public List<Uso> getUsosFincas() throws EyraDaoException {
        Session session = null;
        List<Uso> usos = null;
        try {
            session = HibernateUtil.getSession();
            session.beginTransaction().begin();
            usos = session.createCriteria(Uso.class).list();
            session.beginTransaction().commit();
        } catch (HibernateException e) {
            if (session != null) session.beginTransaction().rollback();
            throw new EyraDaoException(e);
        } finally {
            if (session != null && session.isOpen()) session.close();
        }
        return usos;
    }

    public List<Acuerdo> getAcuerdosPorParque(int id_parque) throws EyraDaoException {
        Session session = null;
        List<Acuerdo> acuerdos = null;
        try {
            session = HibernateUtil.getSession();
            session.beginTransaction().begin();
            acuerdos = session.createCriteria(Acuerdo.class).add(Restrictions.eq("parque.id_parque", id_parque)).list();
            session.beginTransaction().commit();
        } catch (HibernateException e) {
            if (session != null) session.beginTransaction().rollback();
            throw new EyraDaoException(e);
        } finally {
            if (session != null && session.isOpen()) session.close();
        }
        return acuerdos;
    }

    public Afeccion getAfeccion(int id_finca, int id_acuerdo) throws EyraDaoException {
        Session session = null;
        Afeccion afeccion = null;
        try {
            session = HibernateUtil.getSession();
            session.beginTransaction().begin();
            afeccion = (Afeccion) session.createCriteria(Afeccion.class)
                    .add(Restrictions.eq("finca.id_finca", id_finca))
                    .add(Restrictions.eq("acuerdo.id_acuerdo", id_acuerdo))
                    .uniqueResult();
            session.beginTransaction().commit();
        } catch (HibernateException e) {
            if (session != null) session.beginTransaction().rollback();
            throw new EyraDaoException(e);
        } finally {
            if (session != null && session.isOpen()) session.close();
        }
        return afeccion;
    }

    public List<Afeccion> getAfecciones(int id_finca) throws EyraDaoException {
        List<Afeccion> afecciones = null;
        Session session = null;
        try {
            session = HibernateUtil.getSession();
            session.beginTransaction().begin();
            afecciones = session.createCriteria(Afeccion.class).add(Restrictions.eq("finca.id_finca", id_finca)).list();
            session.beginTransaction().commit();
        } catch (HibernateException e) {
            session.beginTransaction().rollback();
            throw new EyraDaoException(e);
        } finally {
            session.close();
        }
        return afecciones;
    }
}
